import 'package:audioplayers/audioplayers.dart';
import 'package:flutter/material.dart';
import 'package:flutter_music/pages/home/models/song.dart';
import 'package:flutter_music/utils/audio_player.dart';
import 'package:flutter_music/utils/utils.dart';

class SongsProvider extends ChangeNotifier {
  /// 对 SongsProvider 类进行构建
  SongsProvider() {
    AudioPlayerUtils.onAudioTotalDuration = (event) {
      _totalDuration = event;
      notifyListeners();
    };

    AudioPlayerUtils.onAudioPositionDuration = (event) {
      _curPositionDuration = event;
      notifyListeners();
    };

    AudioPlayerUtils.onAudioState = (event) {
      _playerState = event;
    };

    AudioPlayerUtils.onPlayerComplete = () {
      _isPlaying = false;
      playNextSong();
    };
  }

  /// 歌曲列表
  final List<Song> _songList = [
    Song(
      songName: 'Alone People',
      albumArtImageName: 'alone_people',
      artistName: 'Mr. Neyo',
      audioName: 'vfineMusic_alonePeople',
    ),
    Song(
      songName: 'My show',
      albumArtImageName: 'my_show',
      artistName: 'Mr. Chance the Rapper',
      audioName: 'vfineMusic_myShow',
    ),
    Song(
      songName: 'Treasure People',
      albumArtImageName: 'treasure_people',
      artistName: 'Mr. ASAP Rocky',
      audioName: 'vfineMusic_treasurePeople',
    ),
  ];

  /// 获取歌曲列表
  List<Song> get songs => _songList;

  /// 当前播放的下角标
  int? _currentPlayIndex;
  int? get currentPlayIndex => _currentPlayIndex;
  set currentPlayIndex(int? index) {
    _currentPlayIndex = index;
    // 进行播放
    if (index != null) {
      play();
    }
  }

  /* 
   * 处理播放 
   */
  /// 状态
  bool _isPlaying = false;
  bool get isPlaying => _isPlaying;

  /// 总时长
  Duration _totalDuration = Duration.zero;
  Duration get totalDuration => _totalDuration;

  /// 音频当前播放到的时长
  Duration _curPositionDuration = Duration.zero;
  Duration get curPositionDuration => _curPositionDuration;

  /// 当前播放状态
  PlayerState _playerState = PlayerState.disposed;
  PlayerState get playerState => _playerState;

  /// 播放
  Future<void> play() async {
    // 获取当前播放音频
    final String audioName = _songList[_currentPlayIndex!].audioName ?? '';
    String path = Utils.getAudioPath(audioName);
    // 播放
    await AudioPlayerUtils.play(path);
    _isPlaying = true;
    // 更新
    notifyListeners();
  }

  /// 暂停播放
  void pause() {
    AudioPlayerUtils.pause();
    _isPlaying = false;
    notifyListeners();
  }

  /// 恢复播放
  void resume() {
    AudioPlayerUtils.resume();
    _isPlaying = true;
    notifyListeners();
  }

  /// 拖动到对应的时长
  void seek(Duration position) {
    AudioPlayerUtils.seek(position);
    notifyListeners();
  }

  /// 播放下一首
  void playNextSong() {
    if (_currentPlayIndex != null) {
      if (_currentPlayIndex! < _songList.length - 1) {
        // 不是最后一首
        currentPlayIndex = _currentPlayIndex! + 1;
      } else {
        // 最后一首，则循环到第一首
        currentPlayIndex = 0;
      }
    }
  }

  /// 播放上一首
  void playPreviousSong() {
    if (_currentPlayIndex! > 0) {
      currentPlayIndex = _currentPlayIndex! - 1;
    } else {
      currentPlayIndex = _songList.length - 1;
    }
  }

  /// 暂停or播放
  void playOrPause() {
    if (_isPlaying) {
      pause();
    } else {
      resume();
    }
  }
}
